package com.ruoyi.archives.service.impl;

import java.util.List;

import com.ruoyi.archives.domain.SelfCheckRecord;
import com.ruoyi.archives.mapper.SelfCheckRecordMapper;
import com.ruoyi.archives.service.ISelfCheckRecordService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 自查记录Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class SelfCheckRecordServiceImpl implements ISelfCheckRecordService {
    @Resource
    private SelfCheckRecordMapper selfCheckRecordMapper;

    /**
     * 查询自查记录
     *
     * @param id 自查记录主键
     * @return 自查记录
     */
    @Override
    public SelfCheckRecord selectSelfCheckRecordById(Long id) {
        SelfCheckRecord item = selfCheckRecordMapper.selectSelfCheckRecordById(id);
        item.setTypeName(item.getType().getTypeName());
        return item;
    }

    /**
     * 查询自查记录列表
     *
     * @param selfCheckRecord 自查记录
     * @return 自查记录
     */
    @Override
    public List<SelfCheckRecord> selectSelfCheckRecordList(SelfCheckRecord selfCheckRecord) {
        if (selfCheckRecord.getParams() != null) {
            if (selfCheckRecord.getParams().containsKey("createTimeStart") && selfCheckRecord.getParams().containsKey("createTimeEnd")) {
                if (selfCheckRecord.getParams().get("createTimeStart") != null && !"".equals(selfCheckRecord.getParams().get("createTimeStart"))
                        && selfCheckRecord.getParams().get("createTimeEnd") != null && !"".equals(selfCheckRecord.getParams().get("createTimeEnd"))) {
                    selfCheckRecord.getParams().put("createTimeStart", selfCheckRecord.getParams().get("createTimeStart") + " 00:00:00");
                    selfCheckRecord.getParams().put("createTimeEnd", selfCheckRecord.getParams().get("createTimeEnd") + " 23:59:59");
                }

            }
        }
        List<SelfCheckRecord> selfCheckRecords = selfCheckRecordMapper.selectSelfCheckRecordList(selfCheckRecord);
        selfCheckRecords.forEach(item -> {
            item.setTypeName(item.getType().getTypeName());
        });
        return selfCheckRecords;
    }

    /**
     * 新增自查记录
     *
     * @param selfCheckRecord 自查记录
     * @return 结果
     */
    @Override
    public int insertSelfCheckRecord(SelfCheckRecord selfCheckRecord) {
        selfCheckRecord.setCreateTime(DateUtils.getNowDate());
        return selfCheckRecordMapper.insertSelfCheckRecord(selfCheckRecord);
    }

    /**
     * 修改自查记录
     *
     * @param selfCheckRecord 自查记录
     * @return 结果
     */
    @Override
    public int updateSelfCheckRecord(SelfCheckRecord selfCheckRecord) {
        selfCheckRecord.setUpdateTime(DateUtils.getNowDate());
        return selfCheckRecordMapper.updateSelfCheckRecord(selfCheckRecord);
    }

    /**
     * 批量删除自查记录
     *
     * @param ids 需要删除的自查记录主键
     * @return 结果
     */
    @Override
    public int deleteSelfCheckRecordByIds(Long[] ids) {
        return selfCheckRecordMapper.deleteSelfCheckRecordByIds(ids);
    }

    /**
     * 删除自查记录信息
     *
     * @param id 自查记录主键
     * @return 结果
     */
    @Override
    public int deleteSelfCheckRecordById(Long id) {
        return selfCheckRecordMapper.deleteSelfCheckRecordById(id);
    }
}
